<?php

namespace App\Repositories;

use App\Models\Goods;
use App\Models\GoodsSlideshow;
use App\Models\Label;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class GoodsRepository
 * @package App\Repositories
 * @version February 18, 2018, 3:18 pm UTC
 *
 * @method Goods findWithoutFail($id, $columns = ['*'])
 * @method Goods find($id, $columns = ['*'])
 * @method Goods first($columns = ['*'])
*/
class GoodsRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'title',
        'abstract',
        'pic',
        'length',
        'wide',
        'category_id',
        'price',
        'gram_weight',
        'describe',
        'read_num',
        'like_num',
        'on_sell',
        'sort'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Goods::class;
    }

    public function saveData($goods,Request $request)
    {
        $picRes = app()->build(PicturesRepository::class);

        $colors = $request->colors;
        if(count($colors)>0){
            $goods->colors()->sync($colors);
        }else{
            $goods->colors()->detach();
        }
        $slide_shows = $request->file("slide_shows");
        if(count($slide_shows)>0){
            // GoodsSlideshow::where('goods_id',$goods->id)->delete();
            foreach ($slide_shows as $slide_show) {
                $_res = $picRes->uploadByHash($slide_show,"goods",null,null);
                GoodsSlideshow::create([
                    'pic'=>$_res->url,
                    'goods_id'=>$goods->id,
                ]);
            }
        }
        $articles = $request->articles;
        if(count($articles)>0){
            $goods->articles()->sync($articles);
        }else{
            $goods->articles()->detach();
        }
        $labels = $request->labels;
        if(count($labels)>0){
            $goods->labels()->sync($labels);
        }else{
            $goods->labels()->detach();
        }

        $products = $request->products;
        if(count($products)>0){
            $goods->products()->sync($products);
        }else{
            $goods->products()->detach();
        }

        $technology_str = $request->technology_str;
        if(count($technology_str)>0){
            $goods->technology = implode(',',$technology_str);
        }else{
            $goods->technology = null;
        }
        $texture_str = $request->texture_str;
        if(count($texture_str)>0){
            $goods->texture = implode(',',$texture_str);
        }else{
            $goods->texture = null;
        }
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $picRes->uploadByHash($_image,"goods",null,null);
            $goods->pic = $_res->url;
        }
        $goods->save();
    }

    public function getLimitGoods($label_idso=null)
    {
        $user = Auth::user();
        $role_ids = DB::table('role_user')->where('user_id',$user->id)->pluck('role_id')->toArray();
        $label_ids = DB::table('role_label')->whereIn('role_id',$role_ids)->pluck('label_id')->toArray();
        $category_ids = DB::table('role_category')->whereIn('role_id',$role_ids)->pluck('category_id')->toArray();
        $good_ids = Goods::whereIn('category_id',$category_ids)->pluck('id')->toArray();
        $labels = Label::whereIn('id',$label_ids)->get();
        foreach ($labels as $label) {
            if($label_idso){
                if($label->limit==0 && in_array($label->id,$label_idso)!==false){
                    $good_ids = array_merge($good_ids,DB::table('goods_label')->where('label_id',$label->id)->pluck('goods_id')->toArray());
                }
            }else{
                if($label->limit==0){
                    $good_ids = array_merge($good_ids,DB::table('goods_label')->where('label_id',$label->id)->pluck('goods_id')->toArray());
                }
            }
        }
        return $good_ids;
    }
}
